﻿//---------------------共享方法---------------------------------
function $id(ele) {
    return document.getElementById(ele);
}

function clickthis(item) {
    $id("id_ipt_temp_id").value = item.id;
    $id("id_ipt_temp_name").value = item.innerHTML;
    tempNode_user = item.parentNode.innerHTML;
    return false;
}

function removeLi(ul) {
    var uls = $id(ul);
    if (uls == undefined || uls.length == 0) return;
    var lid = $id("id_ipt_temp_id").value;
    if (lid == "") return;
    var count = uls.children.length;
    for (var i = 0; i < count; i++) {
        if (uls.children[i].firstChild.id == lid) {
            uls.removeChild(uls.children[i]);
            break;
        }
    }
}

function addLi(ul) {
    var uls = $id(ul);
    if (uls == null || uls == undefined) return;
    var v = document.createElement("li");
    var id = $id("id_ipt_temp_id").value;
    if (id == "") return;

    var a = document.createElement("a");
    a.id = id;
    a.innerHTML = $id("id_ipt_temp_name").value;
    a.setAttribute("onclick", "clickthis(this)");
    a.onclick = function() {
        clickthis(a);
    };
    v.appendChild(a);
    uls.appendChild(v);
}

function clearTemp() {
    $id("id_ipt_temp_id").value = "";
    $id("id_ipt_temp_name").value = "";
    tempNode_user = undefined;
    tempNode_path = undefined;
}

function addToRight() {
    addLi("id_ul_right");
    removeLi("id_ul_left");
    clearTemp();
}

function addAllToRight() {
    moveAll("id_ul_left", "id_ul_right");
    clearTemp();
}

function addToLeft() {
    addLi("id_ul_left");
    removeLi("id_ul_right");
    clearTemp();
}

function moveAll(orign, target) {
    if ($id(orign) == null || $id(target) == null) return;
    var org = $id(orign);
    var tar = $id(target);
    var count = org.children.length;
    if (count == 0) return;
    for (var i = 0; i < count; i++) {
        var temp = org.children[0].cloneNode(true);
        tar.appendChild(temp);
        org.removeChild(org.children[0]);
    }
}

function addAllToLeft() {
    moveAll("id_ul_right", "id_ul_left"); //
    clearTemp();
}

function btnCancel() {
    clearTemp();
    $id("div_float_container").style.display = "none";
}

//---------------------共享方法end----------------------------
var tempNode_path;
var tempNode_user;

//----------------------------------------------------------------
//-------------路径-------------------------------

function addNewPath() {
    //结束活动和回退活动没后续活动
    if (curSelectedAct.getAttribute("wftype") == "actRollback" || curSelectedAct.getAttribute("wftype") == "actEnd") return;

    $id("lb_ident").innerHTML = "PATH";
    $id("id_div_left").innerHTML = "";
    $id("id_ul_right").innerHTML = "";
    var acts = document.getElementsByTagName("wfact");
    if (acts == null || acts.length == 0) return;
    var count = acts.length;
    var htms = "<ul id='id_ul_left' >";

    //-----------------------隐藏当前活动的前置活动和后置活动
    var wfpaths = document.getElementsByTagName("wfpath");
    var pathCount = 0;
    if (wfpaths == null) pathCount = 0;
    else {
        pathCount = wfpaths.length;
    }
    for (var i = 0; i < count; i++) {
        if (curSelectedAct.id == acts[i].id) continue;
        var ident = true;
        for (var r = 0; r < pathCount; r++) {
            if ((wfpaths[r].getAttribute("start") == curSelectedAct.id && wfpaths[r].getAttribute("end") == acts[i].id) || (wfpaths[r].getAttribute("end") == curSelectedAct.id && wfpaths[r].getAttribute("start") == acts[i].id)) {
                ident = false;
                break;
            }
        }
        if (ident) {
            if (acts[i].getAttribute("wftype") == "actCommon") {
                htms += "<li><a  id=\"_" + acts[i].id + "\" onclick=\"clickthis(this)\"> " + acts[i].innerHTML + "</a></li>";
            } else {
                htms += "<li><a  id=\"_" + acts[i].id + "\" onclick=\"clickthis(this)\"> " + acts[i].getAttribute("wfname") + "</a></li>";
            }
        }
    }
    htms += "</ul>";
    $id("id_div_left").innerHTML = htms;

    $id("div_float_container").style.display = "block";
}

function btnModifyPath() {
    var uls = $id("id_ul_right");
    var count = uls.children.length;

    if (count == 0) {
        $id("div_float_container").style.display = "none";
        return;
    }

    var nextAct;
    for (var i = 0; i < count; i++) {
        nextAct = $id("id_ul_right").children[i].children[0];
        if (!nextAct) continue;
        var pathId = "path_" + (newPathId++);

        //1 添加信息到列表
        var tb = $id("id_tb_attr_acts").children[0]; //获取tbody
        var newRow = tb.insertRow(1); // 插入一行
        var cel = newRow.insertCell(0);
        cel.innerHTML = pathId;
        cel.style.display = "none";
        cel = newRow.insertCell(1);
        cel.innerHTML = curSelectedAct.id;
        cel.style.display = "none";

        var tdTemp = newRow.insertCell(2);
        tdTemp.innerHTML = nextAct.innerHTML;
        tdTemp.style.width = "85px";

        var tempNextID = nextAct.id.substring(1, nextAct.id.length);
        var tempType = $id(tempNextID).getAttribute("wftype");
        if (tempType == "routeOrSplit") {
            curSelectedAct.setAttribute("wfout", "Y");
        }

        if (curSelectedAct.getAttribute("wftype") == "routeOrSplit") {
            tdTemp = newRow.insertCell(3);
            tdTemp.innerHTML = "<input type='text' value='' onchange=\"setCondition(this)\" style='width:100px;'  />";

            createCondition(pathId, curSelectedAct.id, tempNextID); //如果当前活动是路由活动,则在网页中添加一条条件
        } else {
            tdTemp = newRow.insertCell(3);
            tdTemp.innerHTML = "";
        }

        tdTemp.style.width = "95px";
        tdTemp = newRow.insertCell(4);
        tdTemp.innerHTML = "<label onclick=\"deletePath(this)\">x</label>";
        tdTemp.style.width = "18px";

        //2.添加连线
        var wfpath = document.createElement("div");
        wfpath.id = pathId;
        wfpath.className = "connector";
        wfpath.className += " " + curSelectedAct.id;
        wfpath.className += " " + tempNextID;
        wfpath.innerHTML = "<img src=\"Images/arrow.gif\" class=\"connector-end\" />";
        wfpath.setAttribute("wftype", "path");
        $id("mainCanvas_innerDiv").appendChild(wfpath);

        var canvas = findCanvas('mainCanvas');
        var newConnection = new Connector(wfpath, canvas);
        newConnection.initConnector();
        canvas.connectors.push(newConnection);
        $id("div_float_container").style.display = "none";

        //---------------保存一条路径在内存中(为了方便些)-------
        wfpath = document.createElement("wfpath");
        wfpath.setAttribute("pathid", pathId);
        wfpath.setAttribute("start", curSelectedAct.id);
        wfpath.setAttribute("end", tempNextID);
        wfpath.id = "wfp_" + pathId;
        $id("id_div_paths_cols").appendChild(wfpath);
        //------------------------------------------------------
    }
}

///创建一条条件

function createCondition(pathId, startNode, endNode) {
    var con = document.createElement("wfcondition");
    con.style.display = "none";
    con.id = "wcf_" + pathId;
    con.setAttribute("wfpath", pathId);
    con.setAttribute("wfstartnode", startNode);
    con.setAttribute("wfendnode", endNode);
    con.innerHTML = "";
    $id("id_div_conditions_cols").appendChild(con);
    /*
     <wfcondition style="display: none;" id="wfc_1" wfpath="pathID">x==3 && x!=2</wfcondition>
    */
}

//删除路径

function deletePath(lab) {
    var pathid = lab.parentNode.parentNode.firstChild.innerHTML;
    if (pathid != null) del(pathid);
    var tr = lab.parentNode.parentNode;
    tr.parentNode.removeChild(tr);
}

function del(pathid) {
    /*
        1.删除页面里面路径
    */

    var path = document.getElementById(pathid + "_1");
    path.parentNode.removeChild(path);
    path = document.getElementById(pathid + "_2");
    path.parentNode.removeChild(path);
    path = document.getElementById(pathid + "_3");
    path.parentNode.removeChild(path);

    //-------删除canvas里面数据
    var canvas = findCanvas('mainCanvas');
    var count = canvas.connectors.length;
    for (var i = 0; i < count; i++) {
        if (canvas.connectors[i].id == pathid) {
            canvas.connectors.splice(i, 1);
            break;
        }
    }

    //-----------移除图片
    var imgs = document.getElementsByTagName("img");
    if (imgs != null && imgs.length > 0) {
        var n = imgs.length;
        for (var i = 0; i < n; i++) {
            if (imgs[i].hasAttribute("wfpath") && imgs[i].getAttribute("wfpath") == pathid) {
                imgs[i].parentNode.removeChild(imgs[i]);
                break;
            }
        }
    }

    //-------------如果路径附带条件 移除条件
    var cons = document.getElementsByTagName("wfcondition");
    for (var i = 0; i < cons.length; i++) {
        if (cons[i].getAttribute("wfpath") == pathid) {
            cons[i].parentNode.removeChild(cons[i]);
        }
    }

    /*
         2.删除内存路径
    */
    var wfpath = document.getElementsByTagName("wfpath");
    count = wfpath.length;
    for (var i = 0; i < count; i++) {
        if (wfpath[i].getAttribute("pathid") == pathid) {
            wfpath[i].parentNode.removeChild(wfpath[i]);
            break;
        }
    }
}

function deleteNode(actId) {
    if (actId == null) return;
    var wfpath = document.getElementsByTagName("wfpath");
    var count = wfpath.length;
    if (count > 0) {
        for (var i = 0; i < count; i++) {
            if (wfpath[i] != undefined && (wfpath[i].getAttribute("start") == actId || wfpath[i].getAttribute("end") == actId)) {
                //不论该路径是后续路径还是前置路径统一删除
                del(wfpath[i].getAttribute("pathid"));
                i--;
                count--;
            }
        }
    }
    //------------删除活动本身
    $id(actId).parentNode.removeChild($id(actId));
    //-----------删除画布里面的该活动
    var canvas = findCanvas('mainCanvas');
    count = canvas.connectors.length;
    for (var i = 0; i < count; i++) {
        if (canvas.blocks[i].id == actId) {
            canvas.blocks.splice(i, 1);
            break;
        }
    }
}

//-------------路径---------end-------------------

//生成左边列表

function createLeftUserList() {
    var dt = ns.GetUsers().value;
    if (dt == null || dt.Rows.length == 0) return;
    var count = dt.Rows.length;
    var htms = "<ul id='id_ul_left' >";
    for (var i = 0; i < count; i++) {
        htms += "<li><a  id=\"" + dt.Rows[i].ID + "\" onclick=\"clickthis(this)\">" + dt.Rows[i].NAME + "</a></li>";
    }
    htms += "</ul>";
    $id("id_div_left").innerHTML = htms;
    createRightUserList();
    $id("div_float_container").style.display = "block";
}

function createRightUserList() {
    //----如果已经存在用户列表 获取该活动已经存在的联系人
    var wfuId = "wfu_" + curSelectedAct.id.replace("node", "");
    var wfu = $id(wfuId);
    if (wfu == null) return false;

    var users = wfu.innerHTML;
    if (users == "") return false;
    users = users.split("[")[0];
    var list = users.split(";");
    var count = list.length;
    if (count < 1) return false;
    var ulr = $id("id_ul_right");
    var ull = $id("id_ul_left");
    for (var l = 0; l < count; l++) {
        /// 移除左边对应选项
        var innercount = ull.children.length;
        for (var i = 0; i < innercount; i++) {
            if (ull.children[i].firstChild.id == list[l]) {
                ulr.appendChild(ull.children[i].cloneNode(true));
                ull.removeChild(ull.children[i]);
                break;
            }
        }
    }
    return true;
}

//-------------授权用户---------end--------------

//---------------活动对应模块----------------------------------

function setModule() {
    var dt = ns.GetModules().value;
    if (dt == null || dt.Rows.length < 1) return;
    $id("lb_ident").innerHTML = "MODULE";
    $id("id_div_left").innerHTML = "";
    $id("id_ul_right").innerHTML = "";
    var count = dt.Rows.length;
    var htms = "<ul id='id_ul_left' >";

    for (var i = 0; i < count; i++) {
        htms += "<li><a  id=\"" + dt.Rows[i].MID + "\" onclick=\"clickthis(this)\">" + dt.Rows[i].FILES + "(" + dt.Rows[i].MNAME + ")</a></li>";
    }
    htms += "</ul>";
    $id("id_div_left").innerHTML = htms;

    $id("div_float_container").style.display = "block";
}

function btnModuleConfirm() {
    var uls = $id("id_ul_right");
    var count = uls.children.length;

    if (count == 0) {
        $id("div_float_container").style.display = "none";
        return;
    }
    //-----------设置模块
    var actid = curSelectedAct.id;
    $id(actid).setAttribute("wfmodule", uls.children[0].children[0].id);
    $id(actid).setAttribute("wfmoddesc", uls.children[0].children[0].innerHTML);
    $id("id_ipt_attr_module").value = uls.children[0].children[0].innerHTML;
    $id("div_float_container").style.display = "none";
}
//----------end 活动对应模块